package org.gcu.filestorage.controller;


import lombok.extern.slf4j.Slf4j;
import org.gcu.filestorage.api.ErrorCode;
import org.gcu.filestorage.pojo.AccountInfo;
import org.gcu.filestorage.pojo.RequestResult;
import org.gcu.filestorage.pojo.UserRegisterInfo;
import org.gcu.filestorage.serive.AccountInfoService;
import org.gcu.filestorage.uitls.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
public class RegisterController {

    private final AccountInfoService accountInfoService;

    @Autowired
    public RegisterController(AccountInfoService accountInfoService) {
        this.accountInfoService = accountInfoService;
    }




    // 账号/手机号/工号/电子邮箱不能重复注册, 待后续扩展私有账号的登录功能
    private boolean isAccountExisted(String account) {
        return this.accountInfoService.queryAccountInfo(account) != null;
    }


    private boolean isPhoneNoExisted(String phoneNo) {
        List<AccountInfo> result = this.accountInfoService.queryAccountInfoByPhoneNo(phoneNo);
        return  result!= null && !result.isEmpty();
    }

    private boolean isEmailExisted(String email) {
        List<AccountInfo> result = this.accountInfoService.queryAccountInfoByEmail(email);
        return  result!= null && !result.isEmpty();
    }

    private boolean isEmployeeIdExisted(String employeeId) {
        List<AccountInfo> result = this.accountInfoService.queryAccountInfoByEmployeeId(employeeId);
        return  result!= null && !result.isEmpty();
    }


    @PostMapping("/register")
    RequestResult<Boolean> newUserRegister(@RequestBody UserRegisterInfo newUserRegisterInfo) {
        log.info("newUserRegister(), newUserInfoStruct="+ newUserRegisterInfo);
        RequestResult<Boolean> requestResult = new RequestResult<>();
        if (isAccountExisted(newUserRegisterInfo.getAccount())) {
            requestResult.setData(false);
            requestResult.setMessage("注册私有账号已存在");
            requestResult.setResCode(ErrorCode.PRIVATE_ACCOUNT_EXISTED);
            return requestResult;
        }
        if (isPhoneNoExisted(newUserRegisterInfo.getPhoneNo())) {
            requestResult.setData(false);
            requestResult.setMessage("注册手机号已存在");
            requestResult.setResCode(ErrorCode.PHONE_NO_EXISTED);
            return requestResult;
        }
        if (isEmployeeIdExisted(newUserRegisterInfo.getEmployeeId())) {
            requestResult.setData(false);
            requestResult.setMessage("注册工号/学号已存在");
            requestResult.setResCode(ErrorCode.EMPLOYEE_ID_EXISTED);
            return requestResult;
        }
        if (isEmailExisted(newUserRegisterInfo.getEmail())) {
            requestResult.setData(false);
            requestResult.setMessage("注册邮箱已存在");
            requestResult.setResCode(ErrorCode.EMAIL_EXISTED);
            return requestResult;
        }

        long registerTime = new Date().getTime();
        String encryptedPassword = MD5Util.encryptByMD5(newUserRegisterInfo.getPassword());
        AccountInfo privateAccountInfo = new AccountInfo(0, newUserRegisterInfo.getAccount(),
                encryptedPassword, newUserRegisterInfo.getRealName(), newUserRegisterInfo.getGender(),
                newUserRegisterInfo.getDepartment(), newUserRegisterInfo.getEmployeeId(), newUserRegisterInfo.getPhoneNo(),
                newUserRegisterInfo.getEmail(), newUserRegisterInfo.getAvatarUrl(), 1, registerTime,
                0, 1);
        boolean success = this.accountInfoService.insertNewAccountInfo(privateAccountInfo);
        if (newUserRegisterInfo.getRegisterPublic() == 1) {
            if (isAccountExisted(newUserRegisterInfo.getPublicAccount())) {
                requestResult.setData(false);
                requestResult.setMessage("注册公用账号已存在");
                requestResult.setResCode(ErrorCode.PUBLIC_ACCOUNT_EXISTED);
                return requestResult;
            }
            String encryptedPublicPassword = MD5Util.encryptByMD5(newUserRegisterInfo.getPublicPassword());
            AccountInfo publicAccountInfo = new AccountInfo(0, newUserRegisterInfo.getPublicAccount(),
                    encryptedPublicPassword, newUserRegisterInfo.getRealName(), newUserRegisterInfo.getGender(),
                    newUserRegisterInfo.getDepartment(), newUserRegisterInfo.getEmployeeId(), newUserRegisterInfo.getPhoneNo(),
                    newUserRegisterInfo.getEmail(), newUserRegisterInfo.getAvatarUrl(), 1, registerTime,
                    0, 2);
            success = this.accountInfoService.insertNewAccountInfo(publicAccountInfo);
        }
        if (success) {
            requestResult.setData(true);
            requestResult.setMessage("注册成功");
            requestResult.setResCode(ErrorCode.SUCCESS);
        } else {
            requestResult.setData(false);
            requestResult.setMessage("注册失败");
            requestResult.setResCode(ErrorCode.REGISTER_ACCOUNT_FAILED);
        }
        return requestResult;
    }
}
